Homework 1#
Exercise 1-1#
a) \(f(x) = cos(x^3)\)
Using the chain rule : \(\left( f(g(x)) \right) = f'(g(x)) \cdot g'(x)\)
b) \(f(x) = x^x\)
c) \(f(x) = e^{sin(2x)}cos(x)\)
d) Compute the derivative (\(\frac{df}{dx}, \frac{df}{dy}\)) of \(f(x,y) = cos(x^2(t) + y^2(t))\), with respect to \(t\), assuming that \(x(t), y(t)\) vary with time. Write the answers in terms of \(\frac{dx}{dt}, \frac{dy}{dt}\).
Recall the multivariate chain rule
Exercise 1-2#
Note
I believe this should be \(-\lambda x\) for a decaying situation
Given that mass \(x\) of a radioactive element obeys the following differential equation in time :
a) Write down the solution to \(x(t)\) describing the rate of decay
Start by taking the integral of both sides
To get the value of \(k\), we can set \(t=0\) and get that \(k = x(0)\), which is our intiial condition.
x_0 = 2
t_range = 1:1:5
lambdas = [5,1,0,0.01,0.1];
function getvals(lambdas::Vector{Float64}, t_range::StepRange{Int,Int},x_0::Int64)
# pre-allocate the output array with the appropriate type and size
# avoid the need for dynamic resizing and multiple heap allocations.
# reduces memory fragmentation and improves performance by ensuring all memory is allocated upfront
out = Vector{Vector{Float64}}(undef,length(lambdas))
for (i,l) in enumerate(lambdas)
# Pre-allocate innnter vector 'for_l' with the correct size to avoid multiple allocations
for_l = Vector{Float64}(undef, length(t_range)+1)
for_l[1] = x_0
for (j,t) in enumerate(t_range)
val = exp(-l*t)*x_0
for_l[j+1] = val #direct assignment of pre-allocated array instead of push
end
out[i] = for_l #direct assignment of pre-allocated array instead of push
end
out
end
getvals (generic function with 1 method)
using Plots
labels = ["λ = $i" for i in lambdas]
p = plot()
for (i,series) in enumerate(getvals(lambdas,t_range,x_0))
plot!(p,series,label = labels[i])
end
display(p)
c) The half-life T is defined as the time it takes for the material to be reduced to half of its mass through radioactive decay. The half-life of uranium-238 is 4.468 billion years. What is the corresponding value of λ?
we need \(\lambda\) such that:
d) If you start with 100kg of uranium-238, how long until you only have 5kg left?
We need \(t\) such that \(x(0) = 100\) and \(x(t) = 5\)
Exercise 1-3#
Compute the Taylor series expansion by hand for f(x). For each function, plot f(x) and the three-term expansion (i.e., the first three nonzero terms) from x = −5 to x = 5.
a ) \(f(x) = \frac{sin(x)}{x}\)
Recall that the Taylor expansion for \(f(x)\) can be written as :
Here, given we want an expansion from -5 to 5, we calculate the taylor expansion from \(a=0\)
that is,
Note
\(\frac{sin(a)}{a}\) is not defined at a = 0, but using limits and L’Hopital’s rule, we can find it to be 1, as follows:
So on and so forth. For brevity, note that the first three terms of the taylor expansion of \(sin(x)\):
thus the first 3 terms of \(f(x)\) :
f_approx(x) = 1 - x^2/factorial(3) + x^4/factorial(5)
function f(x)
return x == 0 ? 1 : sin(x)/x # if x = 0, return 1 to handle the singularity at 0
end
val_range = -5:0.0001:5
values = f_approx.(val_range);
using Plots
plot(val_range,f.(val_range), label = "Actual")
plot!(val_range,f_approx.(val_range), label = "Approximation")
b) \(f(x) = 3^x\)
Recall again the taylor expansion around 0:
thus the first 3 terms of \(f(x)\) :
f_approx(x) = 1 - x^2/factorial(3) + x^4/factorial(5)
function f(x)
return x == 0 ? 1 : sin(x)/x # if x = 0, return 1 to handle the singularity at 0
end
val_range = -5:0.0001:5
values = f_approx.(val_range);
using Plots
plot(val_range,f.(val_range), label = "Actual")
plot!(val_range,f_approx.(val_range), label = "Approximation")
Exercise 1-4#
Please compute an analytic expression, by hand, for the real and imaginary parts of the following complex functions. Please also plot these for t=0:.01:10.
a) \(f(t) = e^{it}\)
Recall Euler’s formula of \(e^t\):
#f_approx(x) = 1 - x^2/factorial(3) + x^4/factorial(5)
f(x) = im*sin(x) + cos(x)
val_range = 0:.01:10
plot(val_range, real.(f.(val_range)), label="Real part", lw=2)
plot!(val_range, imag.(f.(val_range)), label="Imaginary part", lw=2)
b) \(f(t) = e^{(-1-i)t}\)
f(t) = -exp(-t)*im*sin(t) + exp(-t)cos(t)
val_range = 0:.01:10
plot(val_range, real.(f.(val_range)), label="Real part", lw=2)
plot!(val_range, imag.(f.(val_range)), label="Imaginary part", lw=2)